<thead>

<?php


include '../../functions.php';
include '../../additionalFunctions.php';

$crop = $_GET['crops'];
$unit = substr($crop, strripos($crop, " - ") + 3); // gets just the name of the unit
$crop = substr($crop, 0, strripos($crop, " - ")); // finds the last position of occurence of " - " ==> takes only the crop name, not the units
$month = $_GET['month'];
$year = $_GET['year'];
$attributes = $_GET['attributes'];
$attributesArray = explode(';',$attributes);

print "<tr><th></th><th>Week</th>";
foreach ($attributesArray as $a) {
	if ($a == "amount_harvested") 
		print "<th>Amount Harvested</th>";
	else if ($a == "amount_donated") 
		print "<th>Amount Donated</th>";
	else if ($a == "sales") 
		print "<th>Sales</th>";
	else
		print "<th>$a</th>";
}
print "</tr></thead><tbody>";

$count = 0;
$resultArray = array();
foreach ($attributesArray as $a) {
	
	$result = getRangeWeeklyCropData($year."-".getMonthNumber($month)."-01",$year."-".getMonthNumber($month)."-".lastDayOfMonth(getMonthNumber($month)),getCropId($crop),$a, getUnitId($unit), $a == 'sales' ? getCustomerId($_GET['customers']) : ($a == 'amount_donated' ? getCustomerId($_GET['donationCustomers']) : NULL));

	$resultArray[$count] = $result;
	$count += 1;
}

$allWeeks = array();
foreach ($resultArray as $ra => $weekValue) {
	foreach ($weekValue as $week => $value) {
		$allWeeks[] = $week;
	}
}

$allWeeksUnique = array_unique($allWeeks);
natcasesort($allWeeksUnique);

foreach ($allWeeksUnique as $w) {
	print "<tr><td id=\"td1\"></td><td id=\"td2\">";
		print $w;
		print "</td>";
		for ($j = 0; $j < count($attributesArray); $j++) {
			$l = $j + 3;
			print "<td id=\"td".$l."\">";
			foreach ($resultArray[$j] as $week => $value) {
				if ($week == $w) {
					print $value;
				}
			}
			print "</td>";
		}
		print "</tr>";
}

/*
foreach ($resultArray as $ra) {
	foreach ($ra as $week => $value) {
		
		print "<tr><td id=\"td1\"></td><td id=\"td2\">";
		print $week;
		print "</td>";
		for ($j = 0; $j < count($attributesArray); $j++) {
			$l = $j + 3;
			print "<td id=\"td".$l."\">".$value[$attributesArray[$j]]."</td>";
		}
		print "</tr>";
	}
}	
*/
?>
</tbody>